import * as bunyan from 'bunyan'
import config from '../config/env'
var fs = require("fs");


let log: bunyan
const initLog = () => {
    checkAndCreateDir(`./logs/`)
    const streams = [];
    streams.push({ level: config.log.level, path: `./logs/cooperation.log`, count: 10, type: 'rotating-file' });
    if (config.enableLogOutputInConsole) {
        streams.push({ level: config.log.level, stream: process.stdout });
    }

    log = bunyan.createLogger({
        name: 'car',
        streams: streams,
        serializers: bunyan.stdSerializers
    });
}

class LogWrapper {
    public debug(message: string, format?: any, ...args: any[]): void;
    public debug(message: Object, format?: any, ...args: any[]): void;
    public debug(message: any, format?: any, ...args: any[]): void {
        let parameters = [message]
        if (format) { parameters.push(format) }
        if (args) { parameters = parameters.concat(args) }
        log.debug.apply(log, parameters)
    }

    public info(message: string, format?: any, ...args: any[]): void;
    public info(message: Object, format?: any, ...args: any[]): void;
    public info(message: any, format?: any, ...args: any[]): void {
        let parameters = [message]
        if (format) { parameters.push(format) }
        if (args) { parameters = parameters.concat(args) }
        log.info.apply(log, parameters)
    }


    public warn(message: string, format?: any, ...args: any[]): void;
    public warn(message: Object, format?: any, ...args: any[]): void;
    public warn(message: any, format?: any, ...args: any[]): void {
        let parameters = [message]
        if (format) { parameters.push(format) }
        if (args) { parameters = parameters.concat(args) }
        log.warn.apply(log, parameters)
    }

    public error(error: Error, format?: any, ...args: any[]): void;
    public error(error: string, format?: any, ...args: any[]): void;
    public error(error: Object, format?: any, ...args: any[]): void;
    public error(error: any, format?: any, ...args: any[]): void {
        let parameters = [error]
        if (format) { parameters.push(format) }
        if (args) { parameters = parameters.concat(args) }
        log.error.apply(log, parameters)
    }


    public fatal(fatal: Error, format?: any, ...args: any[]): void;
    public fatal(fatal: string, format?: any, ...args: any[]): void;
    public fatal(fatal: Object, format?: any, ...args: any[]): void;
    public fatal(fatal: any, format?: any, ...args: any[]): void {
        let parameters = [fatal]
        if (format) { parameters.push(format) }
        if (args) { parameters = parameters.concat(args) }
        log.fatal.apply(log, parameters)
    }
}

function checkAndCreateDir(dir) {
    if (!fs.existsSync(dir)) {
        fs.mkdirSync(dir);
    }
}

initLog();

const logUtil = new LogWrapper()

export {
    logUtil
}